package com.hzgj.bcl.soa.ws;

import com.alibaba.fastjson.JSON;
import com.hzgj.bcl.soa.collector.JmxCollector;
import com.hzgj.bcl.soa.command.CommandsInitializer;
import com.hzgj.bcl.soa.command.RegistryCommand;
import com.hzgj.bcl.soa.command.UpOrDownServiceCommand;
import com.hzgj.bcl.soa.registry.RegistryUpdater;
import com.hzgj.bcl.util.lang.StrKit;
import lombok.Getter;
import lombok.Setter;

import java.util.Collection;
import java.util.concurrent.BlockingQueue;

import static com.hzgj.bcl.soa.command.CommandsInitializer.REG_COMMAND;
import static com.hzgj.bcl.soa.command.CommandsInitializer.UPORDOWNSERVICE_COMMAND;

public class UpDownServiceHandler extends MessageHandler {

    @Getter
    private String name;

    @Getter
    @Setter
    static class UpDownObject {
        String address;
        String jport;
        String type;
        String service;
    }

    @Override
    void handle(String parameters, BlockingQueue<byte[]> queue) {
        this.name = parameters;
        UpDownObject object = JSON.parseObject(parameters, UpDownObject.class);

        String host = object.getAddress().split(":")[0];
        UpOrDownServiceCommand upOrDownServiceCommand = CommandsInitializer.getCommand(UPORDOWNSERVICE_COMMAND);

        boolean up = "0".equals(object.getType()) ? true : false;
        Integer count = 1;
        JmxCollector jmxCollector = upOrDownServiceCommand.getJmxCollector(host, Integer.parseInt(object.getJport()));
        if (StrKit.isBlank(object.getService())) {
            RegistryCommand registryCommand = CommandsInitializer.getCommand(REG_COMMAND);
            Collection<RegistryCommand.NodeProvider> providers = registryCommand.getNodeServices(object.getAddress());
            for (RegistryCommand.NodeProvider provider : providers) {
                if (provider.getProvider().isUp() ^ up) {
                    try {
                        RegistryUpdater.Result result = upOrDownServiceCommand.jmxInvoke(jmxCollector, provider.getServiceName(), object.getAddress(), !up);
                        queue.offer(getMessage(count, result.getInfo()));
                    } catch (Exception e) {
                        logger.error("{}", e);
                        queue.offer(getMessage(count, (up ? " up " : " down ") + provider.getServiceName() + " failure," + e.getMessage()));
                    }
                } else {
                    queue.offer(getMessage(count, "need not " + (up ? " up " : " down ") + provider.getServiceName()));
                }
                count++;
            }
        } else {
            try {
                RegistryUpdater.Result result = upOrDownServiceCommand.jmxInvoke(jmxCollector, object.getService(), object.getAddress(), !up);
                queue.offer(getMessage(count, result.getInfo()));
            } catch (Exception e) {
                logger.error("{}", e);
                queue.offer(getMessage(count, (up ? " up " : " down ") + object.getService() + " failure," + e.getMessage()));
            }
        }
        queue.offer(getMessage(-1, "ws_quit"));
    }

    byte[] getMessage(int count, String message) {
        return ("{ \"count\": " + count + ", \"message\": \"" + message + "\"}").getBytes();
    }
}